package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunTopicMember;
import com.ruoyi.quanzi.service.ISunTopicMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 话题成员Controller
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/quanzi/member")
public class SunTopicMemberController extends BaseController
{
    @Autowired
    private ISunTopicMemberService sunTopicMemberService;

    /**
     * 查询话题成员列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunTopicMember sunTopicMember)
    {
        startPage();
        List<SunTopicMember> list = sunTopicMemberService.selectSunTopicMemberList(sunTopicMember);
        return getDataTable(list);
    }

    /**
     * 导出话题成员列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:export')")
    @Log(title = "话题成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunTopicMember sunTopicMember)
    {
        List<SunTopicMember> list = sunTopicMemberService.selectSunTopicMemberList(sunTopicMember);
        ExcelUtil<SunTopicMember> util = new ExcelUtil<SunTopicMember>(SunTopicMember.class);
        util.exportExcel(response, list, "话题成员数据");
    }

    /**
     * 获取话题成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunTopicMemberService.selectSunTopicMemberById(id));
    }

    /**
     * 新增话题成员
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:add')")
    @Log(title = "话题成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunTopicMember sunTopicMember)
    {
        return toAjax(sunTopicMemberService.insertSunTopicMember(sunTopicMember));
    }

    /**
     * 修改话题成员
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:edit')")
    @Log(title = "话题成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunTopicMember sunTopicMember)
    {
        return toAjax(sunTopicMemberService.updateSunTopicMember(sunTopicMember));
    }

    /**
     * 删除话题成员
     */
    @PreAuthorize("@ss.hasPermi('quanzi:member:remove')")
    @Log(title = "话题成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunTopicMemberService.deleteSunTopicMemberByIds(ids));
    }

    /**
     * 用户加入话题
     */
    @PostMapping("/join/{topicId}")
    public AjaxResult joinTopic(@PathVariable Long topicId)
    {
        Long userId = getUserId();
        return toAjax(sunTopicMemberService.joinTopic(userId, topicId));
    }

    /**
     * 用户退出话题
     */
    @DeleteMapping("/leave/{topicId}")
    public AjaxResult leaveTopic(@PathVariable Long topicId)
    {
        Long userId = getUserId();
        return toAjax(sunTopicMemberService.leaveTopic(userId, topicId));
    }

    /**
     * 获取话题的成员列表
     */
    @GetMapping("/topic/{topicId}")
    public TableDataInfo getTopicMembers(@PathVariable Long topicId)
    {
        startPage();
        List<SunTopicMember> list = sunTopicMemberService.selectMembersByTopicId(topicId);
        return getDataTable(list);
    }

    /**
     * 获取用户加入的话题列表
     */
    @GetMapping("/user/{userId}")
    public TableDataInfo getUserTopics(@PathVariable Long userId)
    {
        startPage();
        List<SunTopicMember> list = sunTopicMemberService.selectTopicsByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 检查用户是否已加入某个话题
     */
    @GetMapping("/check/{topicId}")
    public AjaxResult checkMembership(@PathVariable Long topicId)
    {
        Long userId = getUserId();
        boolean isMember = sunTopicMemberService.isUserMemberOfTopic(userId, topicId);
        return success(isMember);
    }

    /**
     * 获取话题的成员数量
     */
    @GetMapping("/count/{topicId}")
    public AjaxResult getMemberCount(@PathVariable Long topicId)
    {
        int count = sunTopicMemberService.getMemberCountByTopicId(topicId);
        return success(count);
    }

    /**
     * 设置成员角色
     */
    @PutMapping("/role/{memberId}/{role}")
    public AjaxResult setMemberRole(@PathVariable Long memberId, @PathVariable String role)
    {
        return toAjax(sunTopicMemberService.setMemberRole(memberId, role));
    }

    /**
     * 获取话题管理员列表
     */
    @GetMapping("/admins/{topicId}")
    public TableDataInfo getTopicAdmins(@PathVariable Long topicId)
    {
        startPage();
        List<SunTopicMember> list = sunTopicMemberService.selectAdminsByTopicId(topicId);
        return getDataTable(list);
    }
}